using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using EPiServer.Shell.Gadgets;
using EPiServer.Shell.Web;
using EPiServer.Core;
using EPiServer.Web.Hosting;
using System.Web.Hosting;
using EPiServer.Web;
using EPiServer.Research.MobileCenter.GadgetHelper;
using EPiServer.Research.MobileCenter.JSON;





namespace EPiServer.Research.MobileCenter.Examples.Controllers
{
    /// <summary>
    /// Create a news Gadget
    /// </summary>
    [Gadget(IconUrl="/modules/MobileCenterExampleGadgets/Content/Icon2.png", Name="CreateNews", Title="Create News")]
    [CssResource("/modules/MobileCenterExampleGadgets/Content/createpages.css")]
    public class CreateNewsController : MobileCenterJsonBase
    {
        public ActionResult Index()
        {

            List<SelectListItem> list = new List<SelectListItem>();

            SelectListItem s1 = new SelectListItem();
            s1.Selected = true;
            s1.Text = "Save and Publish";
            s1.Value = "Save and Publish";

            SelectListItem s2 = new SelectListItem();
            s2.Selected = false;
            s2.Text = "Save";
            s2.Value = "Save";

            list.Add(s1);
            list.Add(s2);
            ViewData.Add("DropDownOptions", list.ToList());
                

            return View();
        }

 

        /// <summary>
        /// Creates the pages.
        /// </summary>
        /// <param name="PageName">Name of the page.</param>
        /// <param name="Writer">The writer.</param>
        /// <param name="MainBody">The main body.</param>
        /// <param name="photouploaded">The photouploaded.</param>
        /// <param name="Action">The save action.</param>
        /// <returns></returns>
        public ActionResult CreatePages(string Heading, string MainContent, string position, string photouploaded, string Action)
        {


            return Index();
            if (String.IsNullOrEmpty(Heading))
            {
                string errormessage = "Page Name is required and cannot be empty";
                ViewData.Add("Error", errormessage);

              
                
            }
            else
            {
              try{

                  /* var settings = new 

                   string HeadingPropertyName = settings.PageTitle;
                   string MainContentPropertyName = settings.TextContent;
                   string MapContentPropertyName = settings.MapContent;
                   string ImageContentPropertyName = settings.ImageContent;
                   PageReference pf = new PageReference(settings.StartPointPageID);
                   string PageTypePropertyName = settings.PageType;

                   if (pf == null)
                   {
                       string errormessage = "No News root page";
                       ViewData.Add("Error", errormessage);

                       return Index();
                   }

                   PageData pd = DataFactory.Instance.GetDefaultPageData(pf, PageTypePropertyName);

                   //Get the page folder ID 
                   pd[HeadingPropertyName] = Heading;


                   MainContent = HttpUtility.HtmlEncode(MainContent);

                   pd[MainContentPropertyName] = MainContent;

                   pd[MapContentPropertyName] += position;


                   UnifiedDirectory pageDirectory = pd.GetPageDirectory(true);

                   if (!String.IsNullOrEmpty(photouploaded))
                   {
                       UnifiedFile file = System.Web.Hosting.HostingEnvironment.VirtualPathProvider.GetFile(photouploaded) as UnifiedFile;

                       file.CopyTo(pageDirectory.VirtualPath + "MobileCenteruploadedimage.png");

                       photouploaded = pageDirectory.VirtualPath + "MobileCenteruploadedimage.png";

                       if (photouploaded != null)
                       {
                          pd[ImageContentPropertyName] +=  "<br /><img src=\"" + photouploaded + "\" alt=\"photo\" width=\"300px\" />";
                       }
                   }
                   if (Action == "Save and Publish")
                   {
                       DataFactory.Instance.Save(pd, EPiServer.DataAccess.SaveAction.Publish);
                   }
                   else
                   {
                       DataFactory.Instance.Save(pd, EPiServer.DataAccess.SaveAction.Save);
                   }
                   return View("Saved", pd);*/
                  return Index();
              }
                catch (Exception e)
                {
                    string errormessage = "Error creating page";
                    ViewData.Add("Error", errormessage + e.InnerException);
                    
                }

               
            }
        }

        /// <summary>
        /// Gets a value indicating whether [should return list].
        /// </summary>
        /// <value><c>true</c> if [should return list]; otherwise, <c>false</c>.</value>
        public override bool ShouldReturnList
        {
            get
            {
                return false;
            }
        }

        /// <summary>
        /// Gets the list view.
        /// </summary>
        /// <returns></returns>
        public override List<CellView> GetListView()
        {
            throw new NotImplementedException();
        }



        /// <summary>
        /// Gets the web view.
        /// </summary>
        /// <returns></returns>
        public override WebView GetWebView()
        {
            WebView g = new WebView();
            g.Name = "Create News";
            g.UseDefaultUrl = true;
            return g;
        }
      
    }
}
